/**
 * @file field
 * @author fanxueliang@qq.com
 */
import type {ListSpecItem, NormalListField, LikeFieldType, ExtensionsAttr} from '../interface';
import {getBasicField} from '../util';

export function creatListField(spec: ListSpecItem, showIndex: number, path: string) {
    const rules = spec.rules;
    if (rules) {
        if (rules.minCount || rules.count) {
            rules.required = true;
        }
    }
    let extensions: ExtensionsAttr[] = [];
    if (spec?.extraAttr?.extensions) {
        extensions = spec.extraAttr.extensions;
    }
    let component = 'Tabs';
    if (spec.ui) {
        component = spec.ui;
    }
    const listChildren: LikeFieldType[] = [];

    const basicField = getBasicField(spec, path, showIndex);
    return {
        ...basicField,
        extensions: extensions,
        type: 'LIST',
        isLeaf: false,
        spec,
        component,
        children: listChildren,
        hasVirtualBlock: spec.items?.length && spec.element
    } as NormalListField;
}
